# -*- coding: utf-8 -*-
from enum import Enum
from typing import Tuple, Dict
from PIL import Image, ImageFont, ImageDraw
import wx

from assets.font import FontPathEnum
from infrastructure.constant.Image_size_enum import ImageSizeEnum


class Str2PilImgConfigItem:
    def __init__(self, width: int=16, hight: int=16, text_xy:Tuple[float, float]=(3.2, 0.8), font: str = "arial.ttf", font_size: int = 14, color="#000000", backgroud_color: Tuple[int, int, int]=(255, 255, 255)):
        self.width = width
        self.hight = hight
        self.text_xy = text_xy
        self.font = font
        self.font_size = font_size
        self.color = color
        self.backgroud_color = backgroud_color


class Str2PilImgConfigEnum(Enum):
    # key的命名规则：[背景色(透明背景为：)]_[字体颜色]_[图片尺寸width]_[图片尺寸height]_[多少个字符][字符类型：c:中文, e:英文，d:数字]
    transparent_white_16_16_1e = Str2PilImgConfigItem(width=16, hight=16, text_xy=(0.9, -3), font=FontPathEnum.Arial.value, font_size=20, color="#ffffff", backgroud_color=(255, 255, 255, 0))
    transparent_white_32_32_1e = Str2PilImgConfigItem(width=32, hight=32, text_xy=(0.5, -6), font=FontPathEnum.Arial.value, font_size=40, color="#ffffff", backgroud_color=(255, 255, 255, 0))
    transparent_white_64_64_1e = Str2PilImgConfigItem(width=64, hight=64, text_xy=(0.7, -15), font=FontPathEnum.Arial.value, font_size=84, color="#ffffff", backgroud_color=(255, 255, 255, 0))

    transparent_white_16_16_1c = Str2PilImgConfigItem(width=16, hight=16, text_xy=(0, 0), font=FontPathEnum.SimHei.value, font_size=16, color="#ffffff", backgroud_color=(255, 255, 255, 0))
    transparent_white_32_32_1c = Str2PilImgConfigItem(width=32, hight=32, text_xy=(0, 0), font=FontPathEnum.SimHei.value, font_size=32, color="#ffffff", backgroud_color=(255, 255, 255, 0))
    transparent_white_64_64_1c = Str2PilImgConfigItem(width=64, hight=64, text_xy=(0, 0), font=FontPathEnum.SimHei.value, font_size=64, color="#ffffff", backgroud_color=(255, 255, 255, 0))

    @classmethod
    def get(cls, name):
        return Str2PilImgConfigEnum.__dict__.get(name, None)

    @classmethod
    def get_name(cls, image_value:str, size:ImageSizeEnum = ImageSizeEnum.s16X16) -> str:
        if image_value is None or len(image_value) == 0:
            return None
        image_value = image_value[0]
        key = "transparent_white"
        key = f"{key}_{size.value[0]}_{size.value[1]}"
        if image_value.isalpha():
            key = f"{key}_{len(image_value)}e"
        else:
            key = f"{key}_{len(image_value)}c"
        return key


def str2pilimg(text: str, config:Str2PilImgConfigEnum = Str2PilImgConfigEnum.transparent_white_16_16_1e) -> Image:
    if not text or len(text) == 0 or not config:
        return None
    config = config.value
    image = Image.new("RGBA", (config.width, config.hight), config.backgroud_color)
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype(config.font, config.font_size)
    draw.text(config.text_xy, text[0], font=font, fill=config.color)
    return image


def str2wximg(text: str, config:Str2PilImgConfigEnum = Str2PilImgConfigEnum.transparent_white_16_16_1e) -> wx.Image:
    pil_image = str2pilimg(text=text, config=config)
    wx_image = wx.Image(config.value.width, config.value.hight)
    wx_image.SetData(pil_image.convert("RGBA").tobytes())
    return wx_image